package com.pet.web.controller.wechat;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.pet.common.core.controller.BaseController;
import com.pet.common.core.domain.AjaxResult;
import com.pet.common.core.domain.entity.SysUser;
import com.pet.common.core.page.TableDataInfo;
import com.pet.framework.wechat.WechatService;
import com.pet.system.domain.PetCart;
import com.pet.system.domain.PetProduct;
import com.pet.web.controller.system.vo.CustomerBody;
import com.pet.web.controller.wechat.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信登录验证
 *
 * @author pet
 */
@Api(value = "微信接口", tags = "微信小程序的接口管理")
@RestController
@RequestMapping("/wechat")
@Transactional(rollbackFor = Exception.class)
public class WechatController extends BaseController {
    @Autowired
    private WechatService wechatService;

    /**
     * 登录方法
     *
     * @param wxLoginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @ApiImplicitParam(name = "wxLoginBody", value = "wxLoginBody", required = true, dataTypeClass = WxLoginBody.class)
    @PostMapping("/login")
    public AjaxResult login(@RequestBody WxLoginBody wxLoginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        JSONObject login = wechatService.login(wxLoginBody.getJs_code());
        ajax.put("loginResult", login);
        return StringUtils.isNotBlank(login.getString("openid")) ? ajax : AjaxResult.error(login.getString("errmsg"));
    }

    /**
     * 登录之后，补充客户信息
     *
     * @param customerBody 客户信息
     * @return 结果
     */
    @ApiOperation("登录之后，补充客户信息")
    @ApiImplicitParam(name = "customerBody", value = "customerBody", dataTypeClass = CustomerBody.class)
    @PostMapping("/updateCustomer")
    public AjaxResult updateCustomer(@RequestBody CustomerBody customerBody) {
        // 生成令牌
        int update = wechatService.updateCustomer(customerBody.getOpenid(), customerBody.getNickName());
        return toAjax(update);
    }

    /**
     * 获取手机号码
     *
     * @param wxPhoneBody code
     * @return 结果
     */
    @ApiOperation("获取手机号")
    @ApiImplicitParam(name = "wxPhoneBody", value = "wxPhoneBody", required = true, dataTypeClass = WxPhoneBody.class)
    @PostMapping("/getMobile")
    public AjaxResult getMobile(@RequestBody WxPhoneBody wxPhoneBody) {
        AjaxResult ajax = AjaxResult.success();
        String phoneNum = wechatService.getMobile(wxPhoneBody.getOpenid(), wxPhoneBody.getCode());
        ajax.put("phoneNum", phoneNum);
        return StringUtils.isBlank(phoneNum) ? AjaxResult.error() : ajax;
    }

    /**
     * 获取商户列表
     *
     * @param wxBody 参数openid
     * @return 结果
     */
    @ApiOperation("获取商户列表")
    @ApiImplicitParam(name = "wxBody", value = "wxBody", required = true, dataTypeClass = WxBody.class)
    @PostMapping("/getMerchantList")
    public AjaxResult getMerchantList(@RequestBody WxBody wxBody) {
        AjaxResult ajax = AjaxResult.success();
        List<SysUser> userList = wechatService.getMerchantList(wxBody.getOpenid());
        ajax.put("merchantList", userList);
        return ajax;
    }

    /**
     * 加入商家
     *
     * @param wxMerchantBody 参数openid,商家码
     * @return 结果
     */
    @ApiOperation("加入商家")
    @ApiImplicitParam(name = "wxMerchantBody", value = "wxMerchantBody", required = true, dataTypeClass = WxMerchantBody.class)
    @PostMapping("/addMerchant")
    public AjaxResult addMerchant(@RequestBody WxMerchantBody wxMerchantBody) {
        JSONObject result = wechatService.addMerchant(wxMerchantBody.getOpenid(), wxMerchantBody.getMerchantCode());
        return Integer.valueOf(0).equals(result.getInteger("errcode")) ? AjaxResult.success() : AjaxResult.error(result.getString("errmsg"));
    }

    /**
     * 获取类目列表
     *
     * @param wxBody 参数userId
     * @return 结果
     */
    @ApiOperation("获取类目列表")
    @ApiImplicitParam(name = "wxBody", value = "wxBody", required = true, dataTypeClass = WxBody.class)
    @PostMapping("/getCategoryList")
    public AjaxResult getCategoryList(@RequestBody WxBody wxBody) {
        AjaxResult ajax = AjaxResult.success();
        List<String> categoryList = wechatService.getCategoryList(wxBody.getOpenid());
        ajax.put("categoryList", categoryList);
        return ajax;
    }

    /**
     * 获取品牌列表
     *
     * @param wxBrandListBody 参数userId
     * @return 结果
     */
    @ApiOperation("获取品牌列表")
    @ApiImplicitParam(name = "wxBrandListBody", value = "wxBrandListBody", dataTypeClass = WxBrandListBody.class)
    @PostMapping("/getBrandList")
    public AjaxResult getBrandList(@RequestBody WxBrandListBody wxBrandListBody) {
        List<String> brandList = wechatService.getBrandList(wxBrandListBody.getOpenid(), wxBrandListBody.getUserId(),
                wxBrandListBody.getCategory());
        return AjaxResult.success().put("brandList", brandList);
    }

    /**
     * 获取产品列表
     *
     * @param wxBrandBody 参数brandId
     * @return 结果
     */
    @ApiOperation("获取产品列表")
    @ApiImplicitParam(name = "wxBrandBody", value = "wxBrandBody", dataTypeClass = WxBrandBody.class)
    @PostMapping("/getProductList")
    public AjaxResult getMerchantList(@RequestBody WxBrandBody wxBrandBody) {
        AjaxResult ajax = AjaxResult.success();
        List<PetProduct> productList = wechatService.getProductList(wxBrandBody.getOpenid(),
                wxBrandBody.getBrandName(), wxBrandBody.getCategory(), wxBrandBody.getUserId());
        ajax.put("productList", productList);
        return ajax;
    }

    /**
     * 商户申请
     *
     * @param wxApplyBody 参数
     * @return 结果
     */
    @ApiOperation("商户申请")
    @ApiImplicitParam(name = "wxApplyBody", value = "入参", dataTypeClass = WxApplyBody.class)
    @PostMapping("/merchantApply")
    public AjaxResult merchantApply(@RequestBody WxApplyBody wxApplyBody) {
        String msg = wechatService.merchantApply(wxApplyBody.getOpenid(), wxApplyBody.getUserName(), wxApplyBody.getMobile());
        return null == msg ? AjaxResult.success() : AjaxResult.error(msg);
    }

    /**
     * 查询采购单列表
     *
     * @param wxCartSelectBody
     * @return
     */
    @ApiOperation("查询采购单列表")
    @ApiImplicitParam(name = "wxCartSelectBody", value = "wxCartSelectBody", dataTypeClass = WxCartSelectBody.class)
    @PostMapping("/getCartList")
    public TableDataInfo list(@RequestBody WxCartSelectBody wxCartSelectBody) {
        startPage();
        JSONArray list = wechatService.selectPetCartListWx(wxCartSelectBody.getOpenid(), wxCartSelectBody.getUserId());
        return getDataTable(list);
    }

    /**
     * 添加到采购单
     *
     * @param wxCartBody
     * @return
     */
    @ApiOperation("添加到采购单")
    @ApiImplicitParam(name = "wxCartBody", value = "wxCartBody", dataTypeClass = WxCartBody.class)
    @PostMapping("/addCart")
    public AjaxResult add(@RequestBody WxCartBody wxCartBody) {
        PetCart petCart = new PetCart();
        petCart.setOpenid(wxCartBody.getOpenid());
        petCart.setProductId(wxCartBody.getProductId());
        return toAjax(wechatService.insertPetCart(petCart));
    }

    /**
     * 删除出采购单
     *
     * @param id
     * @return
     */
    @ApiOperation("删除出采购单")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(wechatService.deletePetCartById(id));
    }

    /**
     * 生成采购单记录
     *
     * @param wxOrderBody
     * @return
     */
    @ApiOperation(value = "生成采购单记录")
    @ApiImplicitParam(name = "wxOrderBody", value = "wxOrderBody", dataTypeClass = WxOrderBody.class)
    @PostMapping("/addOrder")
    public AjaxResult add(@RequestBody WxOrderBody wxOrderBody) {
        String cartIds = wxOrderBody.getCartIdList().stream().map(e -> String.valueOf(e)).collect(Collectors.joining(","));
        return toAjax(wechatService.insertPetOrders(wxOrderBody.getOpenid(), cartIds));
    }
}
